package com.qf.wangqing.sportsnews.fragment.headline;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.qf.wangqing.sportsnews.R;
import com.qf.wangqing.sportsnews.activity.MainActivity;
import com.qf.wangqing.sportsnews.adapter.headline.HeadPageAdapter;
import com.qf.wangqing.sportsnews.utils.PagerEnableSlidingPaneLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HeadLineFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener{

    private ViewPager viewPager;
    private RadioGroup group;


    public HeadLineFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_head_line, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = (ViewPager) view.findViewById(R.id.head_viewPager);
        viewPager.addOnPageChangeListener(this);
        group = (RadioGroup) view.findViewById(R.id.radiogroup);



        group.setOnCheckedChangeListener(this);

        initPager();
        ImageView imageView = (ImageView) view.findViewById(R.id.default_head);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity activity = (MainActivity) getActivity();
                PagerEnableSlidingPaneLayout slidingMain= (PagerEnableSlidingPaneLayout) activity.findViewById(R.id.sliding_main);
                if (slidingMain.isOpen()) {
                    slidingMain.closePane();
                }else {
                    slidingMain.openPane();
                }
            }
        });

    }
    private void initPager() {
        DanymicFragment danymicFragment = new DanymicFragment();
        HeadheadFragment headheadFragment = new HeadheadFragment();

        List<Fragment> list = new ArrayList<>();
        list.add(headheadFragment);
        list.add(danymicFragment);

        HeadPageAdapter adapter = new HeadPageAdapter(getActivity().getSupportFragmentManager(), list);
        viewPager.setAdapter(adapter);


    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.left:
                viewPager.setCurrentItem(0);
                group.setBackgroundResource(R.mipmap.ic_switch_right);
                Toast.makeText(getContext(),"了空间找师傅GV不看见我上班GV人v",Toast.LENGTH_SHORT).show();
                break;
            case R.id.right:
                viewPager.setCurrentItem(1);
                group.setBackgroundResource(R.mipmap.ic_switch_left);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        group.check(group.getChildAt(position).getId());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
